package com.qidian.mes.cal.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mes.cal.mapper.CalTeamMapper;
import com.qidian.mes.cal.service.CalTeamService;
import com.qidian.mes.pojo.cal.CalTeam;
import com.qidian.mes.util.pojo.MESResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 04冯虹丹
 * @Company 起点编程
 * @Time 2023/10/26 16:15
 * @Description
 */
@Service
@Transactional
public class CalTeamServiceImpl implements CalTeamService {
    @Autowired
    private CalTeamMapper calTeamMapper;
    @Override
    public MESResult get(Long teamId) {
        System.out.println("2.======///////***********======="+teamId);
        CalTeam calTeam = null;
        if((calTeam = calTeamMapper.get(teamId)) != null){
            return MESResult.ok(calTeam);
        }
        return MESResult.empty();
    }

    @Override
    public MESResult list(int page, int limit, CalTeam calTeam) {
        PageHelper.startPage(page,limit);
        List<CalTeam> list = calTeamMapper.list(calTeam);
        PageInfo<CalTeam> pageInfo = new PageInfo<>(list);
        return MESResult.ok(pageInfo);
    }

    @Override
    public MESResult save(CalTeam calTeam) {
        if(calTeamMapper.selectByCalTeamName(calTeam.getTeamName()) != null || calTeamMapper.selectByCalTeamCode(calTeam.getTeamCode()) != null){
            return MESResult.duplicate("班组编号或班组名称重复");
        }

        System.out.println("/////////////***********========"+calTeamMapper.getType("168"));
        return MESResult.updateIsOk(calTeamMapper.save(calTeam));
    }

    @Override
    public MESResult deleteByCalTeamId(Long teamId) {
        return MESResult.updateIsOk(calTeamMapper.deleteByCalTeamId(teamId));
    }

    @Override
    public MESResult deleteByTeamIds(Long [] teamId) {
        int count = calTeamMapper.deleteByTeamIds(teamId);
        return MESResult.ok(count + "");
    }

    @Override
    public MESResult update(CalTeam calTeam) {
        CalTeam calTemp = calTeamMapper.selectByCalTeamName(calTeam.getTeamName());
        if(calTemp != null && !calTemp.getTeamId().equals(calTeam.getTeamId())){
            return MESResult.duplicate("班组名称重复");
        }
        return MESResult.updateIsOk(calTeamMapper.update(calTeam));
    }
}
